package com.designpatterns.servicelocatorpattern;

// 服务定位器，去缓存里找服务
public class ServiceLocator
{
	private static Cache cache;

	static
	{
		cache = new Cache();
	}

	public static Service getService(String jndiName)
	{

		Service service = cache.getService(jndiName);

		if (service != null)
		{
			return service;
		}

		// 缓存里没有这个服务，则去InitialContext里面创建
		InitialContext context = new InitialContext();
		Service service1 = (Service) context.lookup(jndiName);
		// 将创建的服务加到缓存里面
		cache.addService(service1);
		return service1;
	}
}
